package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbDict;
import com.bwie.service.TbDictService;
import com.bwie.mapper.TbDictMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.WeakHashMap;

/**
* @author 31803
* @description 针对表【tb_dict】的数据库操作Service实现
* @createDate 2025-07-19 08:36:29
*/
@Service
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict>
    implements TbDictService{

    @Autowired
    TbDictMapper tbDictMapper;

    @Autowired
    StringRedisTemplate redisTemplate;


    @PostConstruct
    public void getLoadDictList(){
        System.out.println("初始化字典表");
        List<TbDict> list = list();
        for (TbDict tbDict : list) {
            String key="dictList:"+tbDict.getDictType();
            if(redisTemplate.hasKey(key)){
                redisTemplate.delete(key);
            }
            redisTemplate.opsForList().leftPush(key, JSON.toJSONString(tbDict));


        }
        System.out.println("结束初始化字典表");

    }

    @Override
    public R getDictPage(PageInfoVo pageInfoVo) {
        //构造分页
        Page<TbDict> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //构造查询条件
        QueryWrapper<TbDict> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbDict::getDictType);
        //搜索数据
        Page<TbDict> tbDictPage = tbDictMapper.selectPage(page, wrapper);

        return R.OK(tbDictPage);
    }
}




